<?php
namespace app\index\controller;

use app\admin\model\ClockModel;
use app\admin\model\DepartmenModel;
use app\admin\model\IntegralModel;
use app\admin\model\JurisdictionModel;
use app\admin\model\LeaveModel;
use app\admin\model\NoticeModel;
use app\admin\model\Page;
use app\admin\model\PositionModel;
use app\admin\model\ProductsModel;
use app\admin\model\ReleaseModel;
use app\admin\model\ReportModel;
use app\admin\model\SettingModel;
use app\admin\model\UserModel;
use app\admin\model\VoucherModel;
use app\index\model\Api;
use think\Db;
use think\helper\Time;
use think\Request;

class Index
{
    public function login(){
        return view('login');
    }
    /**加载首页
     * @param Request $request
     * @return \think\response\View
     */
    public function index(Request $request){
//        Message::send('haha','maowenke');exit;

//        $worker = new Worker();
//        $worker->go();exit;
//        $data = Db::view('user')
//            ->view('company','id as co','company.id=user.company_id','left')
//            ->select();
//        echo '<pre>';
//        print_r($data);exit;
//        return getPwd('123456');
        $user = session('user');
//        echo '<pre>';
//        print_r($user);exit;
        if(!$user){
            return redirect('/index/index/login');
        }
        $ip = ip();
        $setting = SettingModel::getSetting()->where(['card_setting.department_id'=>$user['department_id']])->find();
        $lianjie = 0;
        if($ip==$setting['wifi_ip']){
            $lianjie = 1;
        }
        $notice['com'] = NoticeModel::getNotice(false)->where('notice_type',0)->order('notice.creat_time desc')->limit(3)->select();
        $notice['dep'] = NoticeModel::getNotice()->where('notice_type',$user['department_id'])->order('notice.creat_time desc')->limit(3)->select();
        if($setting){
            if(($setting['work_shift']%3600/60)==0){
                $setting['work_shift'] = floor($setting['work_shift']/3600).':00';
            }else{
                $setting['work_shift'] = floor($setting['work_shift']/3600).':'.($setting['work_shift']%3600/60);
            }
            if(($setting['closing_time']%3600/60)==0){
                $setting['closing_time'] = floor($setting['closing_time']/3600).':00';
            }else{
                $setting['closing_time'] = floor($setting['closing_time']/3600).':'.($setting['closing_time']%3600/60);
            }
        }else{
            $setting = null;
        }

//echo '<pre>';
//print_r($setting);exit;
        return view('index',['user'=>$user,'notice'=>$notice,'lianjie'=>$lianjie,'setting'=>$setting,'newsshu'=>Api::getNews()]);
    }

    /**加载积分榜
     * @param Request $request
     * @return \think\response\View
     */
    public function rankings(Request $request){
//       \think\facade\Cache::clear();
//       return ;
        //当前用户
        $id = session("user.id");
        //公司id
        $company_id = session("user")['company_id'];
        //部门id
        $department_id = session("user")["department_id"];

        //公司排行
        $info = Db::table("zhiu_user")
            ->alias('u')
            ->join(['zhiu_departmen_table' => 'd'], "d.id=u.department_id")
            ->where("u.company_id", $company_id)
            ->order("integral desc")
            ->limit(30)
            ->field('u.*,d.departmen_name')
            ->select();
foreach ($info as $k=>$value){
    if(cache($id.'点赞'.$value['id'])){
        $info[$k]['zan']=1;
    }else{
        $info[$k]['zan']=0;
    }
}
//echo '<pre>';
//        print_r($info);exit;
        //当前用户
        $user = Db::table("zhiu_user")
            ->alias('u')
            ->join(['zhiu_departmen_table' => 'd'], "d.id=u.department_id")
            ->order("integral desc")
            ->field('u.*,d.departmen_name')
            ->select();
        //获取当前用户是属于第几位
        foreach ($user as $k => $v) {
            if ($v["id"] == $id) {
                $my = $k + 1;
                $one[] = $v;
            }
        }

        //部门排行
        $bumeninfo = Db::table("zhiu_user")
            ->alias('u')
            ->join(['zhiu_departmen_table' => 'd'], "d.id=u.department_id")
            ->where("u.company_id", $company_id)
            ->where("u.department_id", $department_id)
            ->order("integral desc")
            ->field('u.*,d.departmen_name')
            ->select();
        foreach ($bumeninfo as $k =>$value){
            $bumeninfo[$k]['zan'] = cache($id.'点赞'.$value['id'])?1:0;
        }
//        echo '<pre>';
//        print_r($bumeninfo);exit;
        //点赞
//        $zan = Db::name("fabulous")->where('be_praised', $id)->field("thumbs_up")->select();
//        if($zan){
//            foreach ($zan as $value) {
//                $zhan[] = $value['thumbs_up'];
//            }
//        }else{
//            $zhan = null;
//        }
        return view('index/rankings', ["info" => $info, "my" => $my, "one" => $one, "bumeninfo" => $bumeninfo,'newsshu'=>Api::getNews()]);
//        if (empty($zan)) {
//            return view('index/rankings', ["info" => $info, "my" => $my, "one" => $one, "bumeninfo" => $bumeninfo,'newsshu'=>Api::getNews()]);
//        } else {
//            foreach ($zan as $value) {
//                $zhan[] = $value['thumbs_up'];
//            }
//            return view('index/rankings', ["info" => $info, "my" => $my, "one" => $one, "zan" => $zhan, "bumeninfo" => $bumeninfo,'newsshu'=>Api::getNews()]);
//        }
    }

    /**加载平台使用
     * @param Request $request
     * @return \think\response\View
     */
    public function platform(Request $request){
//        return strtotime('2018-09-10');
        //获取公司的券
        $data = VoucherModel::getVoucher()
            ->where('status',1)
            ->select();
        //获取公司的发布的额职位
        $zhiwei = ReleaseModel::getRelease()
            ->where('post_release.end_time','>',time())
            ->order('post_release.create_time desc')
            ->limit(5)
            ->select();
//        echo '<pre>';
//        print_r($zhiwei);exit;
        return view('platform',['data'=>$data,'zhiwei'=>$zhiwei,'newsshu'=>Api::getNews()]);
    }

//    /**加载评论页面
//     * @param Request $request
//     * @return \think\response\View
//     */
//    public function msg(Request $request){
//        return view('msg');
//    }

    /**加载个人中心
     * @param Request $request
     * @return \think\response\View
     */
    public function core(Request $request){
        if(!$user = isSign()){
            return redirect('/index/index/login');
        }
        $news = Db::name('news')->where('receive_id',$user['id'])->where('status',0)->count();
        $user = UserModel::getIndexUser()
            ->where('user.id',$user['id'])
            ->find();
        session('user',$user);
//        echo '<pre>';
//        print_r($news);exit;

        return view('core',['user'=>$user,'news'=>$news,'newsshu'=>Api::getNews()]);
    }

    /**加载打卡设置
     * @param Request $request
     * @return \think\response\View
     */
    public function setUp(Request $request){
        $user = session('user');
        //获取本人的权限
        $jurisdiction = JurisdictionModel::getJurisdiction()->where('jurisdiction_name','部门打卡设置')->find();
        if($user['jurisdiction']<$jurisdiction['amounts']){
            return redirect('/index/index/index');
        }
        //获取本部门的打卡设置
        $set = SettingModel::getSetting()->where('card_setting.department_id',$user['department_id'])->find();
//        echo '<pre>';
//        print_r($set);exit;
//        if()
        if($set){
            if(($set['work_shift']%3600/60)==0){
                $set['work_shift'] = floor($set['work_shift']/3600).':00';
            }else{
                $set['work_shift'] = floor($set['work_shift']/3600).':'.($set['work_shift']%3600/60);
            }
            if(($set['closing_time']%3600/60)==0){
                $set['closing_time'] = floor($set['closing_time']/3600).':00';
            }else{
                $set['closing_time'] = floor($set['closing_time']/3600).':'.($set['closing_time']%3600/60);
            }
            $set['overtime'] = $set['overtime']/60;
            $set['late_time'] = $set['late_time']/60;
            $xingqi = [];
            $set['attendances'] = implode(',',$set['attendance']);
            foreach ($set['attendance'] as $value){
                if($value==0){
                    $xingqi[] = '天';
                }
                if($value==1){
                    $xingqi[] = '一';
                }
                if($value==2){
                    $xingqi[] = '二';
                }
                if($value==3){
                    $xingqi[] = '三';
                }
                if($value==4){
                    $xingqi[] = '四';
                }
                if($value==5){
                    $xingqi[] = '五';
                }
                if($value==6){
                    $xingqi[] = '六';
                }
            }
            $set['attendance'] = $xingqi;
        }else{
            $set = null;
        }

//                echo '<pre>';
//        print_r($set);exit;
        return view('setUp',['seting'=>$set,'newsshu'=>Api::getNews()]);
    }

    /**加载考勤页面
     * @param Request $request
     * @return \think\response\View
     */
    public function checkWork(Request $request){
        $user = isSign();
        if(!$user){
            return redirect('/index/index/login');
        }
        $year = $request->param('year',date("Y",time()));
        $month = $request->param('month',date('m',time()));
        $type  = $request->param('type',null);
//        $year = 2018;
//        $month = 9;
        //获取年月的开始和结束时间戳
        $str = $year.'-'.$month;
        $stime = Api::getDays($user['department_id'],$str);
//        echo '<pre>';
//        print_r($stime);exit;
        $clock = [];
        $setting = SettingModel::getSetting()
            ->where('card_setting.department_id',$user['department_id'])
            ->find();
        foreach ($stime as $value){
            if($value<10){
                $value = '0'.$value;
            }
            $start = strtotime($year.'-'.$month.'-'.$value);
            $end = $start+24*3600;
            if($end>time()){
                $end = time();
            }
//                    echo '<pre>';
//        print_r([date('Y-m-d',$start),date('Y-m-d',$end)]);exit;
            if(time()<$start+$setting['work_shift']){
                $clock[date('m月d日',$start)]['toWork'] = '未到打卡时间';
            }else{
                $clock[date('m月d日',$start)]['toWork'] = ClockModel::getClock()
                    ->where('punch_clock.punch_type','toWork')
                    ->where('punch_clock.create_time','>',$start)
                    ->where('punch_clock.create_time','<',$end)
                    ->where('punch_clock.userid',$user['id'])
                    ->find();
            }
            if(time()<$start+$setting['closing_time']){
                $clock[date('m月d日',$start)]['offWork'] = '未到打卡时间';
            }else{
                $clock[date('m月d日',$start)]['offWork'] = ClockModel::getClock()
                    ->where('punch_clock.punch_type','offWork')
                    ->where('punch_clock.create_time','>',$start)
                    ->where('punch_clock.create_time','<',$end)
                    ->where('punch_clock.userid',$user['id'])
                    ->find();
            }

        }
//        foreach ($clock as $k=>$v1){
//            foreach ($v1 as $v=>$va2){
//                $clock[$k][$v]['create_time'] = date('Y-m-d',$clock[$k][$v]['create_time']);
//            }
//        }
//        echo '<pre>';
//        print_r($clock);exit;
        return view('checkWork',['clock'=>$clock,'nian'=>$year,'yue'=>$month,'type'=>$type]);
    }

    /**加载请假页面
     * @param Request $request
     * @return \think\response\View
     */
    public function leave(Request $request){
        if(!$user=isSign()){
            return redirect('/index/index/login');
        }
        return view('leave');
    }

    /**加载工作日报页面
     * @param Request $request
     * @return \think\response\View
     */
    public function daily(Request $request){
        return view('daily');
    }

    /**加载工作周报页面
     * @param Request $request
     * @return \think\response\View
     */
    public function weekly(Request  $request){
        return view('weekly');
    }

    /**加载月总结页面
     * @param Request $request
     * @return \think\response\View
     */
    public function monthly(Request  $request){
        return view('monthly');
    }
    /**加载选择日报，周报，月报提交的人的页面
     * @param Request $request
     * @return \think\response\View
     */
    public function recipient(Request  $request){
        $user = isSign();
        if(!$user){
            return redirect('/index/index/login');
        }
        //获取本公司部门
        $bumen = DepartmenModel::getDepartmen()
            ->where('departmen_table.company_id',$user['company_id'])
            ->select();
        //获取部门的职员
        foreach ($bumen as $value){
            $users[$value['departmen_name']] = UserModel::getIndexUser()
                ->where('user.department_id',$value['id'])
                ->select();
        }
//        echo '<pre>';
//        print_r($users);exit;
        return view('recipient',['users'=>$users]);
    }
    /**加载公告
     * @param Request $request
     * @return \think\response\View
     */
    public function noticeList(Request  $request){
        $name = $request->param('name',null);
        if(!$name){
            return '请输入公告类型';
        }
        $user = session('user');
        if($name=='dep'){
            $notice_type = $user['department_id'];
            $data = Page::Notice(['notice.notice_type'=>$notice_type],'notice.creat_time desc');
        }elseif($name=='com'){
            $data = Page::Notice(['notice.notice_type'=>0],'notice.creat_time desc');
        }else{
            $data = Page::Notice();
        }
        $data['typename'] = $name;
//        $data = Page::Notice();
//        echo '<pre>';
//        print_r($data);exit;
        return view('noticeList',$data);
    }
    /**加载公告详情
     * @param Request $request
     * @return \think\response\View
     */
    public function noticeInfo(Request  $request){
        $id = $request->param('id',null);
        $typename = $request->param('typename',null);
        if(!$id){
            return '请输入要查看详情的id';
        }
        //公司公告
        if($typename=='com'){
            $data = NoticeModel::getNotice(false)->where('notice.id',$id)->find();
            NoticeModel::updateNotice([
                'id'=>$data['id'],
                'readers'=>($data['readers']+1)
            ]);
            $company = Db::name('systems')->where('systems_name','公司名称')->find();
            $str = $company['value'];
        }else{
            //部门公告
            $data = NoticeModel::getNotice()->where('notice.id',$id)->find();
            NoticeModel::updateNotice([
                'id'=>$data['id'],
                'readers'=>($data['readers']+1)
            ]);
            $company = DepartmenModel::getDepartmen()->where('departmen_table.id',$data['notice_type'])->find();
            $str = $company['company_name'].'/'.$company['departmen_name'];
        }
        $data['readers'] = $data['readers']+1;
        return view('noticeInfo',['data'=>$data,'str'=>$str,'typename'=>$typename]);
    }
    /**加载个人详情
     * @param Request $request
     * @return \think\response\View
     */
    public function info(Request  $request){
        $user = session('user');
//        echo '<pre>';
//        print_r($user);exit;
        return view('info',['user'=>$user]);
    }
    /**加载考勤统计
     * @param Request $request
     * @return \think\response\View
     */
    public function attenStatic(Request  $request){
        $user = isSign();
        if(!$user){
            return redirect('/index/index/login');
        }
        //获取查询每日的日期
        $dYear = $request->param('dYear',date("Y",time()));
        $dMonth = $request->param('dMonth',date("m",time()));
        $dDay   = $request->param('dDay',date('d',time()));
        //获取查询每月的日期
        $mYear    = $request->param('mYear',date("Y",time()));
        $mMonth   = $request->param('mMonth',date("m",time()));
        $types     = $request->param('types',1);
        //查询的部门
        $department_id = $request->param('department_id',$user['department_id']);
        $day = $dYear.'-'.$dMonth.'-'.$dDay;
        $a = 0;
        if($day==date('Y-m-d',time())){
            $a=1;
        }
        $month = $mYear.'-'.$mMonth;
        //查询每月上班天数
        $setting = Api::getWorkingDays($department_id,$month);
        $set = SettingModel::getSetting()->where('card_setting.department_id',$department_id)->find();
//        return $setting;
        if(!$setting){
            $setting = 22;
        }
        //获取查询每天的开始和结束时间戳
        $day = strToDay($day);
        //获取查询每月的开始和结束时间戳
        $month = strToMonth($month);
//        echo '<pre>';
//        print_r([date('Y-m-d',$month['start']),date('Y-m-d',$month['end'])]);exit;
        $days = [];
        //查询部门的id
        $users = UserModel::getIndexUser()
            ->where('user.department_id',$department_id)
            ->where('user.status',1)
            ->select();
        $quanqing = 0;
        //查询每日的当天部门的人的记录
        foreach ($users as $u){
            $geren1 = ClockModel::getClock()
                ->where('punch_clock.userid',$u['id'])
                ->where('punch_clock.punch_type','toWork')
                ->where('punch_clock.create_time','>',$month['start'])
                ->where('punch_clock.create_time','<',$month['end'])
                ->where('punch_clock.late',0)
                ->where('punch_clock.absenteeism',0)
                ->where('punch_clock.leave',0)
                ->count();
            $geren2 = ClockModel::getClock()
                ->where('punch_clock.userid',$u['id'])
                ->where('punch_clock.punch_type','offWork')
                ->where('punch_clock.create_time','>',$month['start'])
                ->where('punch_clock.create_time','<',$month['end'])
                ->where('punch_clock.late',0)
                ->where('punch_clock.absenteeism',0)
                ->where('punch_clock.leave',0)
                ->count();
            if($a==1){
                if(time()<$day['start']+$set['work_shift']){
                    $days[$u['nickname']]['toWork']='未到打卡时间';
                }else{
                    $days[$u['nickname']]['toWork'] = ClockModel::getClock()
                        ->where('punch_clock.userid',$u['id'])
                        ->where('punch_clock.punch_type','toWork')
                        ->where('punch_clock.create_time','>',$day['start'])
                        ->where('punch_clock.create_time','<',$day['end'])
                        ->find();
                }
                if(time()<$day['start']+$set['closing_time']){
                    $days[$u['nickname']]['offWork'] = '未到打卡时间';
                }else{
                    $days[$u['nickname']]['offWork'] = ClockModel::getClock()
                        ->where('punch_clock.userid',$u['id'])
                        ->where('punch_clock.punch_type','offWork')
                        ->where('punch_clock.create_time','>',$day['start'])
                        ->where('punch_clock.create_time','<',$day['end'])
                        ->find();
                }
            }else{
                $days[$u['nickname']]['toWork'] = ClockModel::getClock()
                    ->where('punch_clock.userid',$u['id'])
                    ->where('punch_clock.punch_type','toWork')
                    ->where('punch_clock.create_time','>',$day['start'])
                    ->where('punch_clock.create_time','<',$day['end'])
                    ->find();
                $days[$u['nickname']]['offWork'] = ClockModel::getClock()
                    ->where('punch_clock.userid',$u['id'])
                    ->where('punch_clock.punch_type','offWork')
                    ->where('punch_clock.create_time','>',$day['start'])
                    ->where('punch_clock.create_time','<',$day['end'])
                    ->find();
            }
            if($geren1==$setting&&$geren2==$setting){
                $quanqing++;
            }
        }
        //上班打卡正常的条数
        $zhenchang['toWork'] = ClockModel::getClock()
            ->where('punch_clock.departmen_id',$department_id)
            ->where('punch_clock.punch_type','toWork')
            ->where('punch_clock.create_time','>',$month['start'])
            ->where('punch_clock.create_time','<',$month['end'])
            ->where('punch_clock.late',0)
            ->where('punch_clock.absenteeism',0)
            ->where('punch_clock.leave',0)
            ->count();
        //下班正常的条数
        $zhenchang['offWork'] = ClockModel::getClock()
            ->where('punch_clock.departmen_id',$department_id)
            ->where('punch_clock.punch_type','offWork')
            ->where('punch_clock.create_time','>',$month['start'])
            ->where('punch_clock.create_time','<',$month['end'])
            ->where('punch_clock.early_retreat',0)
            ->where('punch_clock.absenteeism',0)
            ->where('punch_clock.leave',0)
            ->count();
        //迟到的条数
        $chidao['late'] = ClockModel::getClock()
                ->where('punch_clock.departmen_id',$department_id)
                ->where('punch_clock.punch_type','toWork')
                ->where('punch_clock.create_time','>',$month['start'])
                ->where('punch_clock.create_time','<',$month['end'])
                ->where('punch_clock.late',1)
                ->where('punch_clock.absenteeism',0)
                ->where('punch_clock.leave',0)
                ->where('punch_clock.overtime',0)
                ->count();
        //早退的条数
        $chidao['early_retreat'] = ClockModel::getClock()
            ->where('punch_clock.departmen_id',$department_id)
            ->where('punch_clock.punch_type','offWork')
            ->where('punch_clock.create_time','>',$month['start'])
            ->where('punch_clock.create_time','<',$month['end'])
            ->where('punch_clock.early_retreat',1)
            ->where('punch_clock.absenteeism',0)
            ->where('punch_clock.leave',0)
            ->where('punch_clock.overtime',0)
            ->count();
        //上班请假的条数
        $qingjia['toWork'] = ClockModel::getClock()
            ->where('punch_clock.departmen_id',$department_id)
            ->where('punch_clock.punch_type','toWork')
            ->where('punch_clock.create_time','>',$month['start'])
            ->where('punch_clock.create_time','<',$month['end'])
            ->where('punch_clock.leave',1)
            ->count();
        //下班请假的条数
        $qingjia['offWork'] = ClockModel::getClock()
            ->where('punch_clock.departmen_id',$department_id)
            ->where('punch_clock.punch_type','offWork')
            ->where('punch_clock.create_time','>',$month['start'])
            ->where('punch_clock.create_time','<',$month['end'])
            ->where('punch_clock.leave',1)
            ->count();
        //总人数
        $zong = count($users);
//        return $setting;
        //上班未打卡的条数
        $wei['toWork']  = $zong*$setting-$qingjia['toWork']-$zhenchang['toWork']-$chidao['late'];
        //下班未打卡的条数
        $wei['offWork'] = $zong*$setting-$qingjia['offWork']-$zhenchang['offWork']-$chidao['early_retreat'];
        //查询的部门信息
        $department     = DepartmenModel::getDepartmen()
            ->where('departmen_table.id',$department_id)->find();
//        echo '<pre>';
//        print_r($days);exit;
        return view('attenStatic',[
            'day'=>[$dYear,$dMonth,$dDay],
            'month'=>[$mYear,$mMonth],
            'data'=>$days,
            'zhenchang'=>$zhenchang,
            'chidao'=>$chidao,
            'qingjia'=>$qingjia,
            'wei'    =>$wei,
            'zong'   =>$zong,
            'department'   =>$department,
            'setting'=>$setting,
            'quanqing'=>$quanqing,
            'types'    =>$types
        ]);
    }
    /**加载公司各种券页面
     * @param Request $request
     * @return \think\response\View
     */
    public function convertibility(Request  $request){
        //获取id
        $type = $request->param('type',null);
        if(!$type||!$user=isSign()){
            return redirect('/index/index/login');
        }
        //获取类型
        $voucher = VoucherModel::getVoucher()
            ->where('id',$type)
            ->find();
        $voucher_type = $voucher['voucher_type'];
        list($start,$end) = Time::month();
//        echo '<pre>';
//        print_r($voucher_type);exit;
        //获取打卡记录
        $data = ClockModel::getClock()
            ->where('punch_clock.userid',$user['id'])
            ->where('punch_clock.create_time','>',$start)
            ->where('punch_clock.create_time','<',$end)
            ->where("punch_clock.".$voucher_type,1)
            ->order('punch_clock.create_time acs')
            ->select();
//                echo '<pre>';
//        print_r($voucher);exit;
        return view('convertibility',['data'=>$data,'voucher'=>$voucher,'user'=>$user]);
    }
    /**加载公司产品页面
     * @param Request $request
     * @return \think\response\View
     */
    public function shopping(Request  $request){
        if(!$user= isSign()){
            return returns(401);
        }
//        return time();
        $data = ProductsModel::getProducts()->where('status',1)->select();
//        echo '<pre>';
//        print_r($data);exit;
        return view('shopping',['data'=>$data]);
    }
    /**加载晋升道路列表页面
     * @param Request $request
     * @return \think\response\View
     */
    public function promotionList(Request  $request){
        $data = ReleaseModel::getRelease()
            ->where('post_release.end_time','>',time())
            ->select();
//        echo '<pre>';
//        print_r($data);exit;
        return view('promotionList',['data'=>$data]);
    }
    /**加载晋升道路详情页面
     * @param Request $request
     * @return \think\response\View
     */
    public function promotionInfo(Request  $request){
        $id = $request->param('id',null);
        if(!$id){
            return returns(403,'请填写要查看的职位的id');
        }
        if(!$user = isSign()){
            return returns(401);
        }
        $data = ReleaseModel::getRelease()
            ->where('post_release.id',$id)
            ->where('post_release.end_time','>',time())
            ->find();
//        echo '<pre>';
//        print_r($data);exit;
        return view('promotionInfo',['data'=>$data]);
    }
    /**加载添加内部招聘页面
     * @param Request $request
     * @return \think\response\View
     */
    public function newPromotion(Request  $request){
        if(!$user = isSign()){
            return redirect('/index/index/login');
        }
        //检测是否有权限
        if($user['departmen_name']!=='人事部'){
            return redirect('/index/index/promotionlist');
        }
//        echo '<pre>';
//        print_r($user);exit;
        //获取本公司全部职位
        $data = PositionModel::getPosition()
            ->where('position_table.company_id',$user['company_id'])
            ->where('position_table.status',1)
            ->select();
//        echo '<pre>';
//        print_r($data);exit;
        return view('newPromotion',['data'=>$data]);
    }
    /**加载添加公司产品页面
     * @param Request $request
     * @return \think\response\View
     */
    public function productAdd(Request  $request){
        return view('productAdd');
    }
    /**加载添加公司产品详情页面
     * @param Request $request
     * @return \think\response\View
     */
    public function shoppingInfo(Request  $request){
        $id = $request->param('id',null);
        if(!$id){
            return returns(402,'请输入要查询的id');
        }
        if(!$user = isSign()){
            return returns(401);
        }
        $data = ProductsModel::getProducts()->where('id',$id)->find();
//        echo '<pre>';
//        print_r($data);exit;
        return view('shoppingInfo',['data'=>$data]);
    }
    /**加载积分管理页面
     * @param Request $request
     * @return \think\response\View
     */
    public function coreIntManage(Request  $request){
        if(!$user = isSign()){
            return redirect('/index/index/login');
        }
        $department_id = $request->param('department_id',$user['department_id']);
        $users = UserModel::getIndexUser()->where('user.department_id',$department_id)->select();
        $zhong = 0;
        foreach ($users as $value){
            $zhong = $zhong+$value['integral'];
        }
        $departmen = DepartmenModel::getDepartmen()->where('departmen_table.id',$department_id)->find();
//        echo '<pre>';
//        print_r($departmen);exit;
        return view('coreIntManage',['users'=>$users,'departmen'=>$departmen]);
    }
    /**加载我的积分页面
     * @param Request $request
     * @return \think\response\View
     */
    public function coreIntegral(Request  $request){
        if(!$user=isSign()){
            return redirect('/index/index/login');
        }
        $data = IntegralModel::getIntegral(false)
            ->where('integral_change.userid',$user['id'])
            ->order('integral_change.create_time desc')
            ->select();
//        echo '<pre>';
//        print_r($data);exit;
        $user = UserModel::getIndexUser()->where('user.id',$user['id'])->find();
        $fen=0;
        foreach ($data as $value){
            if($value['number']>0){
                $fen = $fen+$value['number'];
            }
        }
        session('user',$user);
        $user['jiafen'] = $fen;
        return view('coreIntegral',['data'=>$data,'user'=>$user]);
    }
    /**加载消息分类页面
     * @param Request $request
     * @return \think\response\View
     */
    public function coreMsg(Request  $request){
        if(!$user=isSign()){
            return redirect('/index/index/login');
        }
//        $rsu = Db::connect();
//        $data['zong']   = $rsu->name('news')->where('receive_id',$user['id'])->where('status',0)->select();
        $data['putong_new'] = Db::view('news',true)
            ->view('user','nickname','user.id=news.send_out','left')
            ->where('news.receive_id',$user['id'])
            ->where('news.status',0)
            ->where('news.news_type','<>','post_release')
            ->order('news.cretae_time desc')
            ->select();
        $data['putong_used'] = Db::view('news',true)
            ->view('user','nickname','user.id=news.send_out','left')
            ->where('news.receive_id',$user['id'])
            ->where('news.status',1)
            ->where('news.news_type','<>','post_release')
            ->order('news.cretae_time desc')
            ->select();
        $data['jinshen_new'] = Db::view('news',true)
            ->view('user','nickname','user.id=news.send_out','left')
            ->where('news.receive_id',$user['id'])
            ->where('news.status',0)
            ->where('news.news_type','post_release')
            ->order('news.cretae_time desc')
            ->select();
        $data['jinshen_used'] = Db::view('news',true)
            ->view('user','nickname','user.id=news.send_out','left')
            ->where('news.receive_id',$user['id'])
            ->where('news.status',1)
            ->where('news.news_type','post_release')
            ->order('news.cretae_time desc')
            ->select();
        $data['putongshu']    = count($data['putong_new']);
        $data['jinshenshu']   = count($data['jinshen_new']);
//                echo '<pre>';
//        print_r($data);exit;
        return view('coreMsg',['data'=>$data,'newsshu'=>Api::getNews()]);
    }
    /**加载消息详情页面
     * @param Request $request
     * @return \think\response\View
     */
    public function message(Request  $request){
        if(!$user=isSign()){
            return redirect('/index/index/login');
        }
        $id = $request->param('id',null);
        if(!$id){
            return redirect('/index/index/coremsg');
        }
        $dataD = Db::name('news')->where('id',$id)->find();
//        if($dataD['news_type']=='post_release'){
//            return 1;
//        }elseif($dataD['news_type']=='work_report'){
//            return 2;
//        }
//        return $dataD;
        Db::name('news')->where('id',$id)->update(['status'=>1]);
//                echo '<pre>';
//        print_r($dataD);exit;
        //如果消息是报告消息
        if($dataD['news_type']=='work_report'){
            $dataC = ReportModel::getReport()
                ->where('work_report.id',$dataD['table_id'])
                ->find();
            $user = UserModel::getIndexUser()->where('user.id',$dataD['send_out'])->find();
//            echo '<pre>';
//            print_r($dataC);exit;
            $title = $dataD['describe'];
            return view('coreMsgInfo1',['data'=>$dataC,'user'=>$user,'id'=>$dataD['id'],'title'=>$title]);
        } elseif($dataD['news_type']=='post_release'){
            //如果消息是晋升消息
            $user = UserModel::getIndexUser()->where('user.id',$dataD['send_out'])->find();
            $dataC = ReleaseModel::getRelease()->where('post_release.id',$dataD['table_id'])->find();
            $title = '职位申请';
            return view('coreMsgInfo',['data'=>$dataC,'id'=>$dataD['id'],'user'=>$user,'title'=>$title]);
        } elseif($dataD['news_type']=='leave'){
            //如果消息是请假消息
//            echo '<pre>';
//            print_r($dataD);exit;
            $user = UserModel::getIndexUser()->where('user.id',$dataD['send_out'])->find();
//            return $dataD['table_id'];
            $dataC = LeaveModel::getLeave()->where('leave.id',$dataD['table_id'])->find();
            $title = $dataD['describe'];
//                        echo '<pre>';
//            print_r($dataD);exit;
            return view('leaveInfo',['data'=>$dataC,'id'=>$dataD['id'],'user'=>$user,'title'=>$title,'thisuser'=>session('user')]);
        }else{
            //如果消息是普通消息
            $fasong = UserModel::getIndexUser()->where('user.id',$dataD['send_out'])->find();
            $title = '普通消息';
//            echo '<pre>';
//            print_r($dataD);
            return view('coreMsgInfo2',['title'=>$title,'user'=>$fasong,'thisuser'=>$user,'data'=>$dataD,'id'=>$dataD['id']]);
        }
        //$dataC = Db::name('work_report')->where('id',11)->find();
//        echo '<pre>';
//        print_r($dataC);exit;
//        return view($temp);
    }

    /**加载添加公告
     * @param Request $request
     * @return \think\response\View
     */
    public function newComNotice(Request $request){
        $type = $request->param('type','com');
//        return $type;
        return view('newComNotice',['type'=>$type]);
    }

    /**加载加分页面
     * @param Request $request
     * @return \think\response\Redirect|\think\response\View
     */
    public function coreIntManageBonus(Request $request){
        //验证登录
        if(!$user=isSign()){
            return redirect('/index/index/login');
        }
        //验证权限
        if(!$auth = Api::AuthIndex('发放积分')){
            return redirect('/index/index/coreintmanage');
        }
        //获取加分人信息
        $id = $request->param('id',null);
        if(!$id){
            return redirect('/index/index/coreintmanage');
        }
        $jiafenren = UserModel::getIndexUser()->where('user.id',$id)->find();
        return view('coreIntManageBonus',['jiafenren'=>$jiafenren]);
    }

    /**加载加分明细
     * @param Request $request
     * @return \think\response\Redirect|\think\response\View
     */
    public function coreintinfo(Request $request){
        if(!$user=isSign()){
            return redirect('/index/index/login');
        }
        $id = $request->param('id',null);
        if(!$id){
            return redirect('/index/index/coreintegral');
        }
        $data = IntegralModel::getIntegral(false)->where('integral_change.id',$id)->find();
//        echo '<pre>';
//        print_r($data);exit;
        return view('coreintinfo',['data'=>$data]);
    }
}
